package com.ymatou.iapi.trading.sellerquery2.parameter.sp;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;

/**
 * 存储过程：sp_trd_GetAppSellerOrders
 * @author songyefeng
 *
 */
public class Sp_GetAppSellerOrders {
	private int sellerId;
	private Integer orderSellerAcceptedStatus;
	private List<Integer> orderStatus;
	private int lastOrderId;
	private Boolean paidInFull;
	private String timeFrom;
	private String timeTo;
	private Integer top;
	@Deprecated
	private Boolean considerOrderStatus;
	@Deprecated
	private Boolean considerRCOrderEstablish;
	@Deprecated
	private Boolean considerRCAccountPaid;
	@Deprecated
	private Boolean considerRestOrderStatus;
	private int filter = 0;
	private String filterValue = null;

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public void setOrderSellerAcceptedStatus(Integer orderSellerAcceptedStatus) {
		this.orderSellerAcceptedStatus = orderSellerAcceptedStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public void setLastOrderId(Integer lastOrderId) {
		this.lastOrderId = lastOrderId;
	}

	public void setPaidInFull(Boolean paidInFull) {
		this.paidInFull = paidInFull;
	}

	public void setTimeFrom(String timeFrom) {
		this.timeFrom = timeFrom;
	}

	public void setTimeTo(String timeTo) {
		this.timeTo = timeTo;
	}

	public void setTop(Integer top) {
		this.top = top;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	public void setFilter(int filter) {
		this.filter = filter;
	}

	public void setFilterValue(String filterValue) {
		this.filterValue = filterValue;
	}

	public Sp_GetAppSellerOrdersByOrderIds execute() {
		YmtOrdersWapper ordersWapper = new YmtOrdersWapper();

		List<Integer> orderIds = null;
		int rowCount;

		String query = "iBuyerId = " + sellerId;
		//		StringBuffer sb = new StringBuffer();
		//		if (paidInFull != null || DataUtil.isNullOrEmpty(orderStatus) == false) {
		//			query += " and (";
		//			if (paidInFull != null) {
		//				sb.append(" (o.iTradingStatus = " + orderSellerAcceptedStatus
		//						+ " and o.bPaidInFull = "
		//						+ DataUtil.bool2Int(paidInFull) + ")");
		//			}
		//			if (DataUtil.isNullOrEmpty(orderStatus) == false) {
		//				if (sb.length() > 0) {
		//					sb.append(" or ");
		//				}
		//				sb.append(" o.iTradingStatus in "
		//						+ DataUtil.toInOfSql(orderStatus));
		//			}
		//			query += sb.toString()+" )";
		//		}
		if (DataUtil.isNullOrEmpty(orderStatus) == false) {
			query += " and o.iTradingStatus in "
					+ DataUtil.toInOfSql2(orderStatus);
		}
		if (timeFrom != null) {
			query += " and dAddTime >= '" + timeFrom + "'";
		}
		if (timeTo != null) {
			query += " and dAddTime < '" + timeTo + "'";
		}
		if (lastOrderId != 0) {
			query += " and iOrderId < " + lastOrderId;
		}
		switch (filter) {
			case 1 :
				query += " and o.iOrderId = '" + filterValue + "'";
				break;
			case 2 :
				query += " and o.sBuyerLoginId = '" + filterValue + "'";
				break;
			case 3 :
				query += " and o.sReceivePerson = '" + filterValue + "'";
				break;
			case 4 :
				//			query += " and exists(SELECT  top 1 1 FROM dbo.Ymt_BillCodeSummaryToOrders bco JOIN dbo.Ymt_BillCodeSummary bc ON bco.BillCodeSummaryID = bc.billCodeSummaryID WHERE bc.IsDeleted = 0  and bco.OrderID = o.iOrderId and  bc.BillCode = '"+filterValue+"')";
				query += " and exists(SELECT  top 1 1 FROM dbo.Ymt_BillCodeSummaryToOrders bco JOIN dbo.Ymt_BillCodeSummary bc ON bco.BillCodeSummaryID = bc.billCodeSummaryID WHERE bc.IsDeleted = 0  and bco.OrderID = o.iOrderId and  bc.BillCode like '%"
						+ filterValue + "%')";
		}
		//砍价团订单，在组团成功前，结果不展示。add by sean 03.01
		query += " AND ( b.OrderId IS NULL  OR b.BizStatus =3) order by iOrderId desc";
		List<Map> orders = ordersWapper
				.selectOrders2(
						"top " + top + " iOrderId",
						"LEFT JOIN dbo.Ymt_OrderBiz b ON b.OrderId = o.iOrderId",
						query);
		if (DataUtil.isNullOrEmpty(orders) == false) {
			orderIds = orders.stream().map(x -> (int) x.get("iOrderId"))
					.collect(Collectors.toList());
		}
		rowCount = orders.size();
		Sp_GetAppSellerOrdersByOrderIds sp = new Sp_GetAppSellerOrdersByOrderIds();
		sp.setOrderIds(orderIds);
		sp.setSellerId(sellerId);
		return sp.execute();
	}
}
